package com.template.mongo.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.template.mongo.model.AuthInfo;
import com.template.mongo.service.UserService;

@Controller
public class LoginController {

	@Autowired
	@Qualifier("userDetailService")
	private UserService userService;

	@RequestMapping("/account/login.htm")
	public String loginPage(){
		return "/login/login";
	}

	@RequestMapping(value = "/account/authenticate.htm", method = {
			RequestMethod.POST, RequestMethod.GET })
	public @ResponseBody boolean authenticate(@ModelAttribute AuthInfo authInf) {
		return userService.authenticate(authInf.getUserName(), 
				authInf.getPwd());
	}
	
	@RequestMapping("/account/home.htm")
	@PreAuthorize("hasAnyRole('ROLE_USER')")
	public String redirectAllowRole(){
		return "/home";
	}
}
